/**
 * ----------------------------------------------------------------------------
 * Copyright Jens Konopka
 * 
 * Alle Rechte vorbehalten.
 * Licence: Apache 2.0
 * ----------------------------------------------------------------------------
 *
 * created on 05.03.2012 by jkonopka
 */
package de.sql.toolkit.generator;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.sql.toolkit.Resource;
import de.sql.toolkit.model.Column;
import de.sql.toolkit.model.Table;

/**
 * @author jkonopka
 * 
 */
public class JvModelImportsGenerator extends AbstractJvModelGenerator {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1735027906113328914L;

	/**
	 * @param dstPath
	 * @param table
	 */
	public JvModelImportsGenerator(File dstPath, Table table) {
		super(dstPath, table);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.sql.toolkit.writer.IWriter#write()
	 */
	@Override
	public String generate() throws Exception {
		final StringBuilder content = new StringBuilder();
		for (final String value : getImports()) {
			content.append("import ");
			content.append(value);
			content.append(";\n");
		}
		content.append("import java.io.Serializable;\n");
		content.append("\n");
		return content.toString();
	}

	/**
	 * @return
	 */
	private List<String> getImports() {
		final List<String> list = new ArrayList<String>();
		for (final Column column : getTable().getColumns()) {
			final String value = getImport(column.getType());
			if (value != null) {
				list.add(value);
			}
		}
		return list;
	}

	/**
	 * @param key
	 * @return
	 */
	private String getImport(final String key) {
		if (key.toLowerCase().equals("date")) {
			return Resource.getProperty("java.import.util.Date");
		}
		if (key.toLowerCase().equals("time")) {
			return Resource.getProperty("java.import.sql.Time");
		}
		if (key.toLowerCase().equals("timestamp")) {
			return Resource.getProperty("java.import.sql.Timestamp");
		}
		return null;
	}

}
